package com.ruoyi.business.controller;

import com.ruoyi.business.service.IWsdNotificationsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdNotifications;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 存储通知的基本信息Controller
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/wsd_notifications")
@EnableScheduling // 开启定时任务
public class WsdNotificationsController extends BaseController
{
    @Autowired
    private IWsdNotificationsService wsdNotificationsService;

    /**
     * 查询存储通知的基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:notifications:list')")
    @GetMapping("/list")
    public TableDataInfo list(WsdNotifications wsdNotifications) {
        startPage();
        List<WsdNotifications> list = wsdNotificationsService.selectWsdNotificationsList(wsdNotifications);
        return getDataTable(list);
    }

    /**
     * 导出存储通知的基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:notifications:export')")
    @Log(title = "存储通知的基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdNotifications wsdNotifications)
    {
        List<WsdNotifications> list = wsdNotificationsService.selectWsdNotificationsList(wsdNotifications);
        ExcelUtil<WsdNotifications> util = new ExcelUtil<WsdNotifications>(WsdNotifications.class);
        util.exportExcel(response, list, "存储通知的基本信息数据");
    }

    /**
     * 获取存储通知的基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:notifications:query')")
    @GetMapping(value = "/{notificationsId}")
    public AjaxResult getInfo(@PathVariable("notificationsId") Long notificationsId)
    {
        return success(wsdNotificationsService.selectWsdNotificationsByNotificationsId(notificationsId));
    }

    /**
     * 新增存储通知的基本信息
     */
    @PreAuthorize("@ss.hasPermi('business:notifications:add')")
    @Log(title = "存储通知的基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WsdNotifications wsdNotifications) {
        SysUser user = getLoginUser().getUser();
        wsdNotifications.setSchoolId(user.getSchoolId());
        wsdNotifications.setCreatedBy(user.getNickName());
        return toAjax(wsdNotificationsService.insertWsdNotifications(wsdNotifications));
    }

    /**
     * 修改存储通知的基本信息
     */
    @PreAuthorize("@ss.hasPermi('business:notifications:edit')")
    @Log(title = "存储通知的基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WsdNotifications wsdNotifications)
    {
        SysUser user = getLoginUser().getUser();
        wsdNotifications.setSchoolId(user.getSchoolId());
        wsdNotifications.setUpdatedBy(user.getNickName());
        return toAjax(wsdNotificationsService.updateWsdNotifications(wsdNotifications));
    }

    /**
     * 删除存储通知的基本信息
     */
    @PreAuthorize("@ss.hasPermi('business:notifications:remove')")
    @Log(title = "存储通知的基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{notificationsIds}")
    public AjaxResult remove(@PathVariable Long[] notificationsIds)
    {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        return toAjax(wsdNotificationsService.deleteWsdNotificationsByNotificationsIds(notificationsIds,schoolId));
    }

    /**
     *定时扫描过期的通知
     */
    //@Scheduled(cron = "*/30 * * * * *")//每天30秒执行一次
    @Scheduled(cron = "0 0 0 * * ?")  // 每天午夜12点执行
    public AjaxResult updateNotificationStatus() {
        Boolean success = wsdNotificationsService.updateNotificationStatus();
        if (success) {
            return AjaxResult.success("更新成功");
        } else {
            return AjaxResult.success("没有通知需要更新");
        }
    }
}
